package com.ruoyi.pingan.fymodule.thirdpa.controller;


import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ModelAttribute;



/**
 * 基础Controller
 *
 * @author wangy
 * @since 20222-2-24 16:00
 */
@Slf4j
public class BaseController {
    /*@Autowired
    private RedisTemplate<String, Object> redisTemplate;*/
    /**
     * ThreadLocal确保高并发下每个请求的request，response都是独立的
     */
    private static ThreadLocal<ServletRequest> currentRequest = new ThreadLocal<ServletRequest>();
    private static ThreadLocal<ServletResponse> currentResponse = new ThreadLocal<ServletResponse>();

    /**
     * @author wangy
     * @since 2021/3/5 15:27
     * 在高并发情况下，保障每个线程都能获取自己的req，resp
     */
    @ModelAttribute
    public void initReqAndRep(HttpServletRequest request, HttpServletResponse response) {
        currentRequest.set(request);
        currentResponse.set(response);
    }

    /**
     * @author wangy
     * @since 2021/3/5 15:45
     * 线程安全/获取req
     */
    public HttpServletRequest request() {
        return (HttpServletRequest) currentRequest.get();
    }

    /**
     * @author wangy
     * @Data: 2021/3/5 15:45
     * @Details: 线程安全/获取resp
     */
    public HttpServletResponse response() {
        return (HttpServletResponse) currentResponse.get();
    }



}
